Public Class OrderHistoryReportByType
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents lblTotalNumber As System.Web.UI.WebControls.Label
    Protected WithEvents lblTotalLabel As System.Web.UI.WebControls.Label
    Protected WithEvents txtFromDate As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtToDate As System.Web.UI.WebControls.TextBox
    Protected WithEvents imgBtnGo As System.Web.UI.WebControls.ImageButton
    Protected WithEvents ddlSupplier As System.Web.UI.WebControls.DropDownList    
    Protected WithEvents lblCustomerName As System.Web.UI.WebControls.Label

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Protected WithEvents Header1 As Header
    Protected WithEvents OrderHistoryReportByTypeCtrl1 As OrderHistoryReportByTypeCtrl
    Protected WithEvents ReportFunctionsLinkCtrl1 As ReportFunctionsLinkCtrl

    Public Const ORDER_HISTORY_REPORT_BY_TYPE_SESSION_NAME As String = "MIMS_WRS.Web.OrderHistoryReportByType"

    'Protected ReportListLink As String

#Region " Properties"
    Protected ReadOnly Property CustomerID() As Integer
        Get
            Dim cid As Integer

            If Not Security.IsCustomer Then
                Try
                    cid = CInt(Request.QueryString("cid"))
                Catch ex As Exception
                    cid = -1
                End Try
            Else
                cid = Security.GetCurrentUser.UsIdPk
            End If

            If Not cid > 0 Then
                Throw New ApplicationException("Invalid or missing parameter")
            End If

            Return cid
        End Get
    End Property

    Protected Property ReportListLink() As String
        Get
            Return CStr(ViewState("ReportListLink"))
        End Get
        Set(ByVal Value As String)
            ViewState("ReportListLink") = Value
        End Set
    End Property

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If Not Me.IsPostBack() Then
            '            Security.CustomerOnly()

            Header1.NavigationBar.Add("Order History Report by Container Type", AppRoot() + "/Customer/OrderHistoryReportByType.aspx?cid=" + Me.CustomerID.ToString())
            Header1.NavigationBar.Add("Order History Report by Product", AppRoot() + "/Customer/OrderHistoryReportByProduct.aspx?cid=" + Me.CustomerID.ToString())
            If Not Security.IsCustomer Then
                Header1.NavigationBar.Add("Back to Customer Report List", AppRoot() + "/Internal/Reports.aspx")
                Header1.MenuIndex = EMenuIndex.InternalReport
                ReportListLink = "<td width=""98%"" class=""txtcolor2""><p><span class=""txtsmall""><strong>You are here: </strong></span><a href=" & AppRoot() & "/Internal/Reports.aspx>Customer Report list</a> / History report by type</p></td>"
            Else
                Header1.MenuIndex = EMenuIndex.CustomerHistoryReport
                ReportListLink = "<td width=""98%"" class=""txtcolor2"" align='right'><p><strong><a href=" & AppRoot() + "/Customer/OrderHistoryReportByProduct.aspx?cid=" + Me.CustomerID.ToString() & ">History report by product</a></strong></p></td>"
            End If

            ' get supplier list of all customer's orders
            Me.ddlSupplier.DataSource = COrder.GetSupplierListFromOrders(Me.CustomerID)
            Me.ddlSupplier.DataTextField = "SUPPLIER_CODE"
            Me.ddlSupplier.DataValueField = "LKD_ID_PK"
            Me.ddlSupplier.DataBind()
            Me.ddlSupplier.Items.Insert(0, New ListItem("[All]", "0"))

            Dim ds As DataSet = Me.GetDataReport()
            Session(Me.ORDER_HISTORY_REPORT_BY_TYPE_SESSION_NAME) = ds

            OrderHistoryReportByTypeCtrl1.DataReport = ds.Tables(0)
            OrderHistoryReportByTypeCtrl1.GenerateReport()
            Dim totalRecord As Integer = CInt(ds.Tables(1).Rows(0)(0))
            lblTotalNumber.Text = totalRecord.ToString()
            'If (totalRecord > 1) Then
            '    lblTotalLabel.Text = "ORDERS"
            'Else
            lblTotalLabel.Text = "ORDER(S)"
            'End If

            'Default DateTime values:
            'From 1/8 of current year To 31/7 of next year
            txtFromDate.Text = FormatDate(GetFromDate)
            txtToDate.Text = FormatDate(GetToDate)

            lblCustomerName.Text = CUser.GetName(Me.CustomerID)
        End If
    End Sub

    Private Function GetFromDate() As Date
        Dim dt As Date
        dt = New Date(Date.Now.Year, 8, 1)

        Return dt
    End Function

    Private Function GetToDate() As Date
        Dim dt As Date
        dt = New Date(Date.Now.Year + 1, 7, 31)

        Return dt
    End Function


    Protected Function GetDataReport() As DataSet
        Dim obj As New COrder
        Dim fromDate As DateTime
        Dim toDate As DateTime

        If txtFromDate.Text = "" Then
            fromDate = CDate("01/01/1900")
        Else
            fromDate = CDate(txtFromDate.Text)
        End If

        If txtToDate.Text = "" Then
            toDate = Date.Now
        Else
            toDate = CDate(txtToDate.Text)
        End If

        Return obj.GetOrderHistoryReportByType(fromDate, toDate, Me.CustomerID, CInt(Me.ddlSupplier.SelectedItem.Value))
    End Function

    Private Sub imgBtnGo_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgBtnGo.Click

        Dim ds As DataSet = Me.GetDataReport()
        Session(Me.ORDER_HISTORY_REPORT_BY_TYPE_SESSION_NAME) = ds

        OrderHistoryReportByTypeCtrl1.DataReport = ds.Tables(0)
        OrderHistoryReportByTypeCtrl1.GenerateReport()
        Dim totalRecord As Integer = CInt(ds.Tables(1).Rows(0)(0))
        lblTotalNumber.Text = totalRecord.ToString()
        'If (totalRecord > 0) Then
        '    lblTotalLabel.Text = "ORDERS"
        'Else
        lblTotalLabel.Text = "ORDER(S)"
        'End If
    End Sub

    Private Sub ReportFunctionsLinkCtrl1_ExportClick() Handles ReportFunctionsLinkCtrl1.ExportClick
        Response.Clear()
        Response.ContentType = "application/msexcel"
        Response.AddHeader("content-disposition", "attachment; filename=""" & "OHRT" & "_" & GetRandomFileName() & ".xls""")


        Dim sHeader As String
        ' = "<p align=center>ORDER HISTORY REPORT BY TYPE</p>"
        'sHeader += "<HTML>"
        'sHeader += "<HEAD>"
        'sHeader += "<title>Order History Report By Type</title>"
        'sHeader += "<meta name=GENERATOR content=Microsoft Visual Studio .NET 7.1>"
        'sHeader += "<meta name=CODE_LANGUAGE content=Visual Basic .NET 7.1>"
        'sHeader += "<meta name=vs_defaultClientScript content=JavaScript>"
        'sHeader += "<meta name=vs_targetSchema content=http://schemas.microsoft.com/intellisense/ie5>"
        'sHeader += "</HEAD>"
        'sHeader += "<body>"

        sHeader += "<TABLE cellSpacing=1 cellPadding=3 width=100% border=1>"
        sHeader += "<thead>"
        sHeader += "<tr><td align=center colSpan=6><strong>ORDER HISTORY REPORT BY CONTAINER TYPE</strong></td></tr>"
        sHeader += "<TR>"
        sHeader += "<TD align=center width=21% rowSpan=2><strong>PO Date</strong></TD>"
        sHeader += "<TD align=center width=17% rowSpan=2><strong>Customer Order No. </strong>"
        sHeader += "</TD>"
        sHeader += "<TD align=center colSpan=3 height=19><strong>Container Type </strong>"
        sHeader += "</TD>"
        sHeader += "<TD align=center width=24% rowSpan=2><strong>Invoice Value<br>"
        sHeader += "(USD)</strong></TD>"
        sHeader += "</TR>"
        sHeader += "<TR align=center>"
        sHeader += "<TD width=10%>40'</TD>"
        sHeader += "<TD width=10%>20'</TD>"
        sHeader += "<TD width=10%>40' HC</TD>"
        sHeader += "</TR>"
        sHeader += "</thead>"
        sHeader += "<TBODY>"

        'class=txtcolor2
        Dim sFooter As String = "</TBODY>"
        sFooter += "<tfoot>"
        sFooter += "<TR>"
        sFooter += "<TD align=right bgColor=#eaeaea colSpan=2 height=19><strong>Total Quantity:</strong></TD>"
        sFooter += "<TD align=right width=10% bgColor=#eaeaea><strong>"
        sFooter += OrderHistoryReportByTypeCtrl1.Total40Feet.ToString()
        sFooter += "</strong></TD>"
        sFooter += "<TD align=right width=10% bgColor=#eaeaea><strong>" + OrderHistoryReportByTypeCtrl1.Total20Feet.ToString() + "</strong></TD>"
        sFooter += "<TD align=right width=10% bgColor=#eaeaea><strong>" + OrderHistoryReportByTypeCtrl1.Total40HCFeet.ToString() + "</strong></TD>"
        sFooter += "<TD align=right bgColor=#eaeaea><strong>" + OrderHistoryReportByTypeCtrl1.TotalInvoiceValue.ToString() + "</strong></TD>"
        sFooter += "</TR>"
        sFooter += "</tfoot>"
        sFooter += "</TABLE>"

        'sFooter += "</body>"
        'sFooter += "</HTML>"

        Response.Write(sHeader)
        Response.Write(OrderHistoryReportByTypeCtrl1.GetTBodyHTML)
        Response.Write(sFooter)
        Response.Flush()
        Response.End()
    End Sub
End Class
